// KeyboardStatus.cpp
//
#include "KeyboardStatus.h"

#include <QKeyEvent>

#include <iostream>

using namespace QtFwk;

// -----------------------------------------------------------------------------------------------
KeyboardStatus::KeyboardStatus()
{
	reset();
}

// -----------------------------------------------------------------------------------------------
KeyboardStatus::~KeyboardStatus()
{
}

// -----------------------------------------------------------------------------------------------
void KeyboardStatus::reset() 
{
	//memset(buffer,0,sizeof(bool)*256);
	modifiers = 0;
	keySequence = QKeySequence("");
}

// -----------------------------------------------------------------------------------------------
void KeyboardStatus::update( QKeyEvent *e, bool pressed )
{
#if 0
	unsigned int k = e->nativeVirtualKey();
	
	if( k<256 )
		buffer[k] = pressed;

	Qt::KeyboardModifiers mod = e->modifiers();

	if (modifiers & Qt::ShiftModifier)	 modifiers |= ModifierShift;
	if (modifiers & Qt::ControlModifier) modifiers |= ModifierCtrl;
	if (modifiers & Qt::AltModifier)	 modifiers |= ModifierAlt;
#endif
	modifiers = 0;
	Qt::KeyboardModifiers mod = e->modifiers();

	if (mod & Qt::ShiftModifier)	modifiers |= ModifierShift;
	if (mod & Qt::ControlModifier)	modifiers |= ModifierCtrl;
	if (mod & Qt::AltModifier)		modifiers |= ModifierAlt;
	if (mod & Qt::MetaModifier)		modifiers |= ModifierMeta;

	keySequence = QKeySequence( e->key() );
	//std::cout<< keySequence.toString().toStdString();
}

// -----------------------------------------------------------------------------------------------
unsigned int KeyboardStatus::getCode() const
{
	unsigned int ks = (int)(keySequence);
	
	if( modifiers & ModifierShift ) ks += Qt::SHIFT;
	if( modifiers & ModifierCtrl  ) ks += Qt::CTRL;
	if( modifiers & ModifierAlt   ) ks += Qt::ALT;
	if( modifiers & ModifierMeta  ) ks += Qt::META;

	return ks;
}


// -----------------------------------------------------------------------------------------------
//std::string KeyboardStatus::toString() const
//{
//	std::string s("");
//
//	if( modifiers & ModifierAlt   ) __addOrAppend("Alt",s);
//	if( modifiers & ModifierCtrl  ) __addOrAppend("Ctrl",s);
//	if( modifiers & ModifierShift ) __addOrAppend("Shift",s);
//
//	std::string keyString;
//	for( int i=0; i<256; ++i )
//	{
//		if( buffer[i] )
//		{
//			keyString = getKeyAsString(i);
//			break;
//		}
//	}
//
//	if( !keyString.empty() )
//		__addOrAppend(keyString,s);
//
//	return s;
//}

// -----------------------------------------------------------------------------------------------
//inline std::string KeyboardStatus::getKeyAsString(int k) const
//{
//	char s[8];
//	sprintf(s,"%d",k);
//	return std::string(s);
//}


// -----------------------------------------------------------------------------------------------
//inline void KeyboardStatus::__addOrAppend( const std::string &src, std::string &dst ) const
//{
//	std::string plus("+");
//	if( dst.empty() )		dst = src;
//	else					dst.append(plus+src);		
//}